package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  安心分期-汇合融-成都 全量30
 *
 *  @author 吴佳宝
 */
@ApiPlan(productId = "334", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HuiHeRong implements ApiOrgPhoneMd5ConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setExtendUrl("?md5=" + apiDataExchangeParameter.getPhoneMd5());
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("md5", apiDataExchangeParameter.getPhoneMd5());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 201)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        apiApplyParameter.setExtendUrl("?code=CEK5eQcDP6m1b4JxBar_aa_QQ_cc__cc_");
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("name", apiApplyParameter.getUserName());
            newMap.put("phone", apiApplyParameter.getPhone());
            newMap.put("province", apiApplyParameter.getProvinceName());
            newMap.put("city", apiApplyParameter.getCity());
            newMap.put("money", apiFieldConverter.getLoanAmountInt());
            newMap.put("remark", apiFieldConverter.getExtendInfo());
            newMap.put("sex", apiFieldConverter.getSexText());
            newMap.put("age", apiFieldConverter.getAge());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
